package xcmg.device.service.basic;

import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.DataDictionaryDO;
import xcmg.device.dao.mapper.DataDictionaryMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.basic.DataDictionaryVO;
import xcmg.device.service.vo.basic.DictionaryRelationVO;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by GYJ on 2017-12-27.
 */
@Service
public class DataDictionaryService {
    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    public PageCommonVO<DataDictionaryVO> list(SearchCommonVO<DataDictionaryVO> condition) {
        PageHelperPlus.orderBy("create_date desc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        if (StringUtils.isEmpty(condition.getFilters().getpId())) {
            condition.getFilters().setpId("0");
        }
        PageCommonVO result = new PageCommonVO();
        List<DataDictionaryVO> voList = new ArrayList<>();
        List<DataDictionaryDO> doList = dataDictionaryMapper.list(condition.getFilters());
        for (DataDictionaryDO e : doList) {
            DataDictionaryVO model = new DataDictionaryVO();
            BeanUtils.copyProperties(e, model);
            voList.add(model);
        }
        result.setPageInfo(new PageInfo(doList));
        result.setPageInfoList(voList);
        return result;
    }


    public PageCommonVO<DictionaryRelationVO> listAll(SearchCommonVO<DictionaryRelationVO> condition) {
        PageHelperPlus.orderBy("c.create_date desc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        PageCommonVO result = new PageCommonVO();
        List<DictionaryRelationVO> voList = dataDictionaryMapper.listAll(condition.getFilters());
        result.setPageInfo(new PageInfo<>(voList));
        return result;
    }


    public List<DictionaryRelationVO> searchList(DictionaryRelationVO condition){
        return dataDictionaryMapper.searchList(condition);
    }

    public PageCommonVO searchListPage(SearchCommonVO<DictionaryRelationVO> condition){
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        PageCommonVO<DictionaryRelationVO> result=new PageCommonVO<>();
        List<DictionaryRelationVO> list = dataDictionaryMapper.searchList(condition.getFilters());
        result.setPageInfo(new PageInfo<>(list));
        return result;
    }


    public ErrorCode add(DataDictionaryVO model){
        if(dataDictionaryMapper.count(model)>0){
               return ErrorCode.DataDictionaryIsExists;
        }
        DataDictionaryDO entity=new DataDictionaryDO();
        BeanUtils.copyProperties(model,entity);
        //新增的数据字典默认非私有和有效
        entity.setPrivated((short)0);
        entity.setValid((short)1);
        if(dataDictionaryMapper.add(entity)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    public ErrorCode remove(List<String> ids){
        for (String id:ids) {
            if(dataDictionaryMapper.countChild(id)>0){
                return ErrorCode.DataDictionaryCantRemove;
            }
        }
        if(dataDictionaryMapper.remove(ids)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    public ErrorCode update(DataDictionaryVO model){
        if(dataDictionaryMapper.count(model)>0){
            return ErrorCode.DataDictionaryIsExists;
        }
        DataDictionaryDO entity=new DataDictionaryDO();
        BeanUtils.copyProperties(model,entity);
        if(dataDictionaryMapper.update(entity)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    public DataDictionaryVO item(String id){
        DataDictionaryDO entity=dataDictionaryMapper.item(id);
        if(entity==null){
            return null;
        }
        DataDictionaryVO model=new DataDictionaryVO();
        BeanUtils.copyProperties(entity,model);
        return model;
    }

    /**
     * 查询字典信息
     * @param parentCode String （父级code）
     * @param code String
     * @return DataDictionaryVO
     */
    public DataDictionaryVO itemByCode(String parentCode,String code){
        DataDictionaryDO entity=dataDictionaryMapper.showItemByCode(parentCode,code);
        if(entity==null){
            return null;
        }
        DataDictionaryVO model=new DataDictionaryVO();
        BeanUtils.copyProperties(entity,model);
        return model;
    }

}
